package gateway

import (
	"context"
	"github.com/liserc/opensocket-server/pkg/startrpc"
	"github.com/liserc/opensocket-server/protocol/gateway"
	"github.com/liserc/opensocket-server/tool/discovery"
	"github.com/liserc/opensocket-server/tool/mq/memamq"
	"google.golang.org/grpc"
)

type HubServer struct {
	gateway.UnimplementedGatewayServer
	config    *Config
	webserver WebServer
	queue     *memamq.MemoryQueue
}

func NewHubServer(conf *Config, webserver WebServer) *HubServer {
	return &HubServer{
		config:    conf,
		webserver: webserver,
	}
}

func (s *HubServer) Start(ctx context.Context) error {
	return startrpc.Start(ctx, &s.config.Discovery, &s.config.Gateway.Prometheus, s.config.Gateway.ListenIP,
		s.config.Gateway.RPC.RegisterIP,
		s.config.Gateway.RPC.Port,
		s.config.Share.RpcRegisterName.Gateway,
		&s.config.Share,
		s.config,
		s.initServer,
	)
}

func (s *HubServer) initServer(ctx context.Context, config *Config, disCov discovery.SvcDiscoveryRegistry, server *grpc.Server) error {
	s.webserver.SetDiscoveryRegistry(disCov, config)
	gateway.RegisterGatewayServer(server, s)
	return nil
}

func (s *HubServer) OnlineBatchPushOneMsg(ctx context.Context, req *gateway.OnlineBatchPushOneMsgReq) (*gateway.OnlineBatchPushOneMsgResp, error) {
	return nil, nil
}
